package com.kchouse.homepage;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kchouse.HomepageFragment;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.bean.HostItem;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyUtils;
import com.squareup.picasso.Picasso;

import java.util.Calendar;

public class CalculatorActivity extends BaseActivity implements View.OnClickListener{

    private CalculatorActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private Button bt_starttime;
    private Button bt_endtime;
    int mYear, mMonth, mDay;
    private TextView tv_time;
    private ListView lv_item;
    private LvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        mContent = this;
        intiData();
        intiView();

    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("油耗计算器");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_time.setOnClickListener(this);
        bt_starttime = (Button) findViewById(R.id.bt_starttime);
        bt_starttime.setOnClickListener(this);
        bt_endtime = (Button) findViewById(R.id.bt_endtime);
        bt_endtime.setOnClickListener(this);

        lv_item=(ListView) findViewById(R.id.lv_item);
        adapter = new LvAdapter();
        lv_item.setAdapter(adapter);
    }

    class LvAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = getLayoutInflater().inflate(R.layout.lv_calcu_item, null);
            return view;
        }
    }


    private void intiData() {

        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_starttime:
                showtime(bt_starttime);
                break;
            case R.id.bt_endtime:
                showtime(bt_endtime);
                break;
            case R.id.tv_time:
                showtime(tv_time);
                break;
        }
    }

    /**
     * 时间选择器
     */
    private void showtime(final TextView tv) {
        DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker arg0, int year, int month, int day) {
                tv.setText(year+"-"+(++month)+"-"+day);      //将选择的日期显示到TextView中,因为之前获取month直接使用，所以不需要+1，这个地方需要显示，所以+1
            }
        };
        DatePickerDialog dialog=new DatePickerDialog(mContent, 0,listener,mYear,mMonth,mDay);//后边三个参数为显示dialog时默认的日期，月份从0开始，0-11对应1-12个月
        dialog.show();
    }


}
